package base;

import menu.MenuItem;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import objects.DataObject;
import objects.GruObject;
import objects.visualization.Block;
import objects.visualization.TextBlock;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import static objects.DataObject.*;
import objects.GruClass;

public class Main {
	public static Block menuBlock = new Block();
	public static HashMap<GruObject, List<MenuItem>> menu = new HashMap<GruObject, List<MenuItem>>();
	public static int y = 0;
	public static Block block;
	
	public static void main( String[] args ) {
		int unitSize = 1;
		Graphics.init( 1024, 768, unitSize, true );
		refresh();
		//Graphics.setColor( 1, 1, 1 );
		
		boolean pressed = true;
		
		long time = 0;
		do {
			Graphics.setColor( 0, 0, 0 );
			Graphics.drawRectangle( 0, 0, 1024 / unitSize, 768 / unitSize );
			Block.selected = null;
			Block.drawSelected = false;
			block.draw( 0, y );
			Block.drawSelected = true;
			if( Block.selected != null ) Block.selected.draw( 0, y );
			Graphics.swapBuffers();
			Display.processMessages();
			if( Keyboard.isKeyDown( Keyboard.KEY_DELETE ) ) {
				if( Block.selected != null ) {
					if( Block.selected.slot != null ) {
						Block.selected.slot.remove();
						refresh();
					}
				}
			}
			int DWheel = Mouse.getDWheel();
			if( DWheel > 0 ) y += 50;
			if( DWheel < 0 ) y -= 50;
			if( Mouse.isButtonDown( 0 ) || Mouse.isButtonDown( 1 ) ) {
				if( !pressed && Block.selected != null ) {
					Block.selected.select();
					pressed = true;
				}
			} else {
				pressed = false;
			}
			
			if( time < System.currentTimeMillis() ) {
				time = System.currentTimeMillis() + 60000l;
				save();
			}
		} while( !Display.isCloseRequested() && !Keyboard.isKeyDown( Keyboard.KEY_ESCAPE ) );
		save();
		compile();
	}
	
	
	
	public static void save() {
		saveChunk( "backup/" + (new Date()).toString().replaceAll( ":", "-" ) + ".gru" );
		saveChunk( "main.gru" );		
	}
	

	public static void compile() {
		for( DataObject object : classClass.classObjects ) {
			GruClass gruClass = object.toGruClass();
			PrintWriter writer = null;
			try {
				writer = new PrintWriter( new FileWriter( gruClass.iD + ".java" ) );
			} catch ( IOException ex ) {
			}
			gruClass.print( writer );
			writer.close();
		}
	}

	
	public static String enterString( String string ) {
		Keyboard.poll();
		int menuX = Mouse.getX() - 5;
		int menuY = 763 - Mouse.getY();
		int width = 200;
		int height = Graphics.currentFont.getLineHeight();
		while( true ) {
			Graphics.setColor( 0, 0, 0 );
			Graphics.drawRectangle( 0, 0, 1024, 768 );
			
			Block.selected = null;
			Block.drawSelected = false;
			block.draw( 0, y );
			
			Graphics.setColor( 0.6f, 0.6f, 1f );
			Graphics.drawRectangle( menuX, menuY, width, height );
			Graphics.setColor( 0.25f, 0.25f, 0.5f );
			Graphics.drawEmptyRectangle( menuX, menuY, width, height );
			Graphics.drawText( string, menuX, menuY );
			
			Graphics.swapBuffers();
			
			while( Keyboard.next() ) {
				if( !Keyboard.getEventKeyState() ) continue;
				if( Keyboard.getEventKey() == Keyboard.KEY_BACK ) {
					string = string.substring( 0, string.length() - 1 );
				} else if( Keyboard.getEventKey() == Keyboard.KEY_RETURN ) {
					return string;
				} else {
					char character = Keyboard.getEventCharacter();
					if( character != 0 ) {
						string += character;
					}
				}
			}
		}
	}

	private static void createMenu( GruObject gruClass, MenuItem... menuItems ) {
		menu.put( gruClass, Arrays.asList( menuItems ) );
	}
	
	public static void showMenu( List<MenuItem> list ) {
		int menuX = Mouse.getX() - 5;
		int menuY = -y + 763 - Mouse.getY();
		Block parentBlock = new Block( menuX, menuY, null, null, true );
		Main.menuBlock.children.clear();
		Main.menuBlock.children.add( parentBlock );
		parentBlock.x = menuX;
		parentBlock.y = menuY;
		parentBlock.height = 2 * Block.defaultMargin;
		for ( MenuItem item : list ) {
			Block childBlock = new TextBlock( menuX + Block.defaultMargin, menuY + Block.defaultMargin, item.caption, item, null, false );
			childBlock.width = Graphics.currentFont.getWidth( item.caption ) + Block.defaultMargin * 2;
			childBlock.height = Graphics.currentFont.getLineHeight();
			childBlock.enframe = true;
			childBlock.object = item;
			menuY += childBlock.height;
			parentBlock.width = Math.max( parentBlock.width, childBlock.width + 2 * Block.defaultMargin );
			parentBlock.height += childBlock.height;
			parentBlock.children.add( childBlock );
		}
	}

	public static void refresh() {
		menuBlock.children.clear();
		block = blocks.get( root ).createBlock( root, 0, 0, blocks, false, null );
		block.children.add( menuBlock );
	}
}